package com.sykj.seaflow.warm.core.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.sykj.seaflow.common.base.util.LoginHelper;
import com.warm.flow.core.constant.FlowCons;
import com.warm.flow.core.entity.Node;
import com.warm.flow.core.listener.Listener;
import com.warm.flow.core.listener.ListenerVariable;
import com.warm.flow.core.listener.NodePermission;
import lombok.extern.slf4j.Slf4j;
import com.sykj.seaflow.sys.remote.api.SysUserApi;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 全局权限监听拦截器
 */
@Component
@Slf4j
public class GlobalPermissionListener implements Listener {

    @Override
    public void notify(ListenerVariable variable) {
        log.debug("权限监听开始: {}", variable);

        // 查看源码 需要传入 NodePermission 才行， 所以这里不能直接设置task
        List<NodePermission> nodePermissionList = new ArrayList<>();
        // 获取后面的节点
        Node node = variable.getNode();
        List<Node> nextNodes = variable.getNextNodes();
        String permissionFlag = node.getPermissionFlag();
        // 重新配置用户， 查询匹配的用户id
        if(ObjectUtil.isNotEmpty(permissionFlag)){
            List<String> userIds = convertUserIds(permissionFlag, variable.getInstance().getCreateBy());
            for(String id: userIds) {
                NodePermission nodePermission = new NodePermission();
                nodePermission.setPermissionFlag(id);
                nodePermission.setNodeCode(node.getNodeCode());
                nodePermissionList.add(nodePermission);
            }
        }

//        List<Task> nextTasks = variable.getNextTasks();

        if(ObjectUtil.isEmpty(nodePermissionList)){
            // 没人的话 ，设置为null;   比如角色和部门 需要获取对应的用户id,但是没获取到， 就要清除permissionFlag
            node.setPermissionFlag(null);
        }else{
            variable.setNodePermissionList(nodePermissionList);
        }


    }

    private List<String> convertUserIds(String permissionFlag, String createBy) {

        if(ObjectUtil.isEmpty(permissionFlag)) return null;
        List<String> list = new ArrayList<>();


        String[] split = permissionFlag.split("@@");

        for(String permission : split){
            // 流程发起人  转为用户id
            if(ObjectUtil.equal(permissionFlag, FlowCons.WARMFLOWINITIATOR)) {
                list.add(createBy) ;
            }else{
                String[] split1 = permission.split(":");
                if(ObjectUtil.equal(split1[0],"role")){
                    // 获取角色下的用户
                    List<String> roleIds = Arrays.asList(split1[1].split(","));
                    List<Long> userIdsByRole = SpringUtil.getBean(SysUserApi.class).getUserIdsByRole(roleIds);
                    list.addAll(userIdsByRole.stream().map(String::valueOf).toList());
                }
                if(ObjectUtil.equal(split1[0], "user")){
                    list.addAll(Arrays.asList(split1[1].split(",")));
                }
            }


        };

        // 去重
        return list.stream().distinct().toList();
    }

}
